﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaPortal.GUI.Library;

namespace Burn4You
{
    public class TemplateSelectionScreen : GUIWindow
    {

        #region GUI Controls

        [SkinControlAttribute(1)]        protected GUIButtonControl btnBack = null;

        [SkinControlAttribute(10)]        protected GUIThumbnailPanel ThumbnailPanel = null;

        #endregion

        #region Variables

        /// <summary>
        /// ID of the last window to call this screen.
        /// </summary>
        public static int CallingWindow;
        /// <summary>
        /// This indicates if the users completed the window (by clicking on the next button). If false, ignore the files selected from this window.
        /// </summary>
        public static bool Completed = false;
        /// <summary>
        /// This is the template the user has chosen to use.
        /// </summary>
        public static DiskTemplate ChosenTemplate;

        #endregion

        #region GUIWindow Overloads

        protected override void OnPageLoad()
        {
            Utils.Burn4YouLog.Write("Template Selection Screen: Loading Page");
            Completed = false;

            CallingWindow = GUIWindowManager.GetPreviousActiveWindow();

            FillTemplateThumbnailPanel();

            base.OnPageLoad();
        }

        public override bool Init()
        {
            return Load(GUIGraphicsContext.Skin + @"\Burn4YouTemplateSelectionScreen.xml");
        }

        public override int GetID
        {
            get
            {
                return (int)Screen.TemplateSelection;
            }

        }

        protected override void OnClicked(int controlId, GUIControl control, MediaPortal.GUI.Library.Action.ActionType actionType)
        {
            if (control == ThumbnailPanel)
            {
                ChosenTemplate = (DiskTemplate)ThumbnailPanel.SelectedListItem.TVTag;
                Completed = true;
                Utils.Burn4YouLog.Write("Template Selection Screen: User completed screen.");
                GUIWindowManager.ShowPreviousWindow();
            }

            if (control == btnBack)
            {
                Utils.Burn4YouLog.Write("Template Selection Screen: User closed screen.");
                GUIWindowManager.ShowPreviousWindow();
            }

            base.OnClicked(controlId, control, actionType);
        }

        protected override void OnPageDestroy(int new_windowId)
        {
            Utils.Burn4YouLog.Write("Template Selection Screen: Screen being destroyed.");
            base.OnPageDestroy(new_windowId);
        }

        #endregion

        #region Functions

        void FillTemplateThumbnailPanel()
        {
            ThumbnailPanel.Clear();

            DiskTemplateReader tmplR = new DiskTemplateReader();

            List<DiskTemplate> templates = tmplR.GetTemplates();

            foreach (DiskTemplate tmpl in templates)
            {
                GUIListItem item = new GUIListItem();

                Utils.Burn4YouLog.Write("Template Selection Screen: Path to thumbnail is: " + tmpl.PreviewImage);

                item.ThumbnailImage = tmpl.PreviewImage;
                item.Label = tmpl.Name;
                item.TVTag = tmpl;

                ThumbnailPanel.Add(item);
            }
        }

        #endregion
    }
}
